Echo = $FFEF
Monitor = $FF1F

/*
LDA #$00
JSR printbin      ;  Print $00 in binary
LDA #$FF
JSR printbin      ;  Print $FF in binary
LDA #$AA
JSR printbin      ;  Print $AA in binary
JMP Monitor
*/

  LDX #$00          ;  Initialize counter to 0
  JMP start         ;  Skip over increment first time through the loop

next:
  INX               ;  Increment the counter
start:
  TXA               ;  printbin2 requires the value be in the accumulator
  PHA               ;  Save X (already in the accumulator), before subroutine
  JSR printbin      ;  print the value as binary
  PLA               ;  Retreive X from Stack, place it in the accumulator
  TAX               ;  Copy X from accumulator to X register
  CPX #$FF          ;  If we've reached 255 ($FF), we're done
  BNE next          ;  If not, loop again

  JMP Monitor



/*
Name:               PrintBin
Precondtions:       Byte to print is in Accumulator.
Postcondtions:      The bit string is printed to screen.
Destroys:           A, X, Y, Flags.
Description:        Prints the binary representation of the value in A.
*/

printbin:
  LDY #$00          ;  Initialize counter to 0
  ROR               ;  ROR once so initial setup will mesh with the loop
  TAX               ;  Loop expects value to be in X register

begin:
  TXA               ;  Restore value to Accumulator
  ROL               ;  Rotate next bit into bit 7's location
  BMI neg           ;  If that bit is a 1, then branch to neg
  TAX               ;  The bit is 0; save byte to X
  LDA #$30          ;  Load ASCII '0' for printing
  JSR Echo          ;  Print 0
  JMP continue      ;  Skip over code for printing a 1
neg:
  TAX               ;  Save byte to X
  LDA #$31          ;  Load ASCII '1' for printing
  JSR Echo          ;  Print 1

continue:
  INY               ;  Increment counter
  CPY #$08          ;  Compare counter to 8 (number of bits to print + 1)
  BNE begin         ;  If counter != 8, then not all bits have been printed,
                    ;  so loop again, otherwise continue
                    
  LDA #$0D          ;  Load ASCII Carriage Return (CR)
  JSR Echo          ;  Print CR to screen
  RTS               ;  Exit subroutine
  
  
  